package retrofit.boot.annotation;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by wulg on 2019/11/12
 * description:
 */
@Getter
@Setter
public class RetrofitClientWrapper {

    private String className;

    private String name;

    private String url;

    private String qualifier;

    private boolean primary;

    public RetrofitClientWrapper(String className, String name, String url, String qualifier,boolean primary) {
        this.className = className;
        this.name = name;
        this.url = url;
        this.qualifier = qualifier;
        this.primary = primary;
    }

    public static final class Builder {
        private Class<?> type;
        private String name;
        private String url = "";
        private String qualifier;
        private boolean primary = true ;

        public RetrofitClientWrapper.Builder type(Class<?> type) {
            this.type = type;
            return this;
        }

        public RetrofitClientWrapper.Builder name(String name) {
            this.name = StringUtils.defaultIfBlank(name,"");
            return this;
        }

        public RetrofitClientWrapper.Builder url(String url) {
            this.url = StringUtils.defaultIfBlank(url,"");
            return this;
        }

        public RetrofitClientWrapper.Builder qualifier(String qualifier) {
            this.qualifier = StringUtils.defaultIfBlank(qualifier,"");
            return this;
        }

        public RetrofitClientWrapper.Builder primary(boolean primary) {
            this.primary = primary;
            return this;
        }

        public RetrofitClientWrapper build() {
            if(StringUtils.isBlank(name)){
                throw new IllegalArgumentException("Name must be set");
            }
            if(type == null){
                throw new IllegalArgumentException("Type must be set");
            }else if(!type.isInterface()){
                throw new IllegalArgumentException("Type must be an interface class");
            }
            return new RetrofitClientWrapper(type.getName(), this.name, this.url, this.qualifier, this.primary);
        }
    }
}
